# import db functionality (db, clearselected, pickfile, pickdirectory)
from functions import *
import subprocess

class CDGUI:
    def __init__(self):
        root = Tk()         # initialize GUI window
        
        dbase = db(root)    # initialize database


        frame = Frame(root)
        listbox = Listbox(frame, selectmode=EXTENDED, width=60, heigh=10)
        listbox.pack(side=LEFT, fill=BOTH, expand=1)
        scrollbar = Scrollbar(frame)
        scrollbar.pack(side=RIGHT, fill=BOTH)
        listbox.config(yscrollcommand=scrollbar.set)
        scrollbar.config(command=listbox.yview)

        dbase.lb = listbox  # store listbox in database
        
        btn_remove = Button(text='Remove selected', command=dbase.clearselected)
        btn_remove.pack(side=BOTTOM, fill=BOTH)
        frame.pack(side=BOTTOM, fill=BOTH,expand=1)

        
        frame2 = Frame(root)
        # initialize buttons
        btn_file = Button(text='Load a bookshelf picture to analyze ...', command=dbase.pickfile)
        btn_dir = Button(text='Load a directory with bookshelf pictures to analyze ...', command=dbase.pickdirectory)

        # bind buttons to GUI
        btn_file.pack(side=LEFT,fill=X,expand=1)
        btn_dir.pack(side=RIGHT,fill=X,expand=1)
        frame2.pack(fill=BOTH)
        
        frame3 = Frame(root)
        btn_analyze = Button(text='Analyze!', command=root.destroy)
        btn_analyze.pack(side=RIGHT)
        frame3.pack(side=RIGHT,fill=X)


        #label = Label(root,"Loaded directories:") - not how labels work, apparently

        #label.pack()
        
        self.root = root
        self.db = dbase
    
    def start(self):
        self.root.mainloop()
        
    def ocropus(self, filen):
        (dirname, filename) = os.path.split(filen)
        (filebasename, fileextension) = os.path.splitext(filename)
        
        curdir = filebasename
        if os.path.exists(curdir):
            step0 = subprocess.Popen("rm %s/* -r" % curdir, shell=True, stdout = subprocess.PIPE)
            step0.wait()
            step01 = subprocess.Popen("rmdir %s" % curdir, shell=True, stdout = subprocess.PIPE)
            step01.wait()
        
        step1 = subprocess.Popen("ocropus book2pages %s \"%s\"" % (curdir, filen), shell=True, stdout = subprocess.PIPE)
        step1.wait()
        step2 = subprocess.Popen("ocropus pages2lines %s" % curdir, shell=True, stdout = subprocess.PIPE)
        step2.wait()
        step3 = subprocess.Popen("ocropus lines2fsts %s" % curdir, shell=True, stdout = subprocess.PIPE)
        step3.wait()
        step4 = subprocess.Popen("ocropus fsts2text %s" % curdir, shell=True, stdout = subprocess.PIPE)
        step4.wait()
        step5 = subprocess.Popen("ocropus buildhtml %s > ~/.cdi-data/%s.html" % (curdir, filebasename), shell=True, stdout = subprocess.PIPE)
        step5.wait()
                
    def ocropi(self, files):
        for f in files:
            self.ocropus(f)
    
    def gocr(self, filename):
        (filebasename, fileextension) = os.path.splitext(filename)
        path = os.path.expanduser("~/.cdi-data/")+filebasename+"/"
        filelist = []
        for (dirpath, dirnames, filenames) in os.walk(path):
            for filetemp in filenames:
                if filetemp.endswith(".jpg") or filetemp.endswith(".jpeg") \
                or filetemp.endswith(".png") or filetemp.endswith(".gif")  \
                or filetemp.endswith(".tif"):
                     proc = subprocess.Popen("gocr "+dirpath+"/"+filetemp, shell=True, stdout = subprocess.PIPE)
                     proc.wait()
                     string = repr(proc.communicate()[0])
                     if len(string)>2:
                         print filetemp+": "+string
